package com.example.springboot01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot01.entity.Class;
import com.example.springboot01.entity.Professor;
import com.example.springboot01.entity.Student;
import com.example.springboot01.entity.User;
import com.example.springboot01.execs.CustomException;
import com.example.springboot01.mapper.ClassMapper;
import com.example.springboot01.model.LoginStatusModel;
import com.example.springboot01.model.constants.UserType;
import com.example.springboot01.req.PageQueryReq;
import com.example.springboot01.resp.enums.CommonRespEnum;
import com.example.springboot01.service.ClassService;
import com.example.springboot01.service.ProfessorService;
import com.example.springboot01.service.StudentService;
import com.example.springboot01.service.TeacherClassService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherClassService classService;

    @Resource
    private ProfessorService professorService;

    @Override
    public List<Class> queryByProfessor(Integer pId) {
        return this.list(new LambdaQueryWrapper<Class>()
                .eq(Class::getProfessorId, pId)
        );
    }

    @Override
    public boolean removeClass(Integer id) {
        List<Student> students = studentService.queryByClass(id);
        if (students.isEmpty()) {
            this.removeById(id);
            return true;
        }
        return false;
    }

    @Override
    public IPage<Class> getPage(PageQueryReq pageQueryReq, HttpSession session) {
        IPage<Class> result = new Page<>(pageQueryReq.getCurrent(), pageQueryReq.getSize());
        Object status = session.getAttribute("status");
        if (status == null) {
            throw new CustomException(CommonRespEnum.UNAUTHORIZED);
        } else {
            LoginStatusModel model = (LoginStatusModel) status;
            Integer userType = model.getUserType();

            if (userType == UserType.TEACHER) {
                List<Integer> classId = classService.queryClassIdByTeacher(model.getId());
                if (!classId.isEmpty()) {
                    result = this.page(result, new LambdaQueryWrapper<Class>().in(Class::getId, classId));
                }
            } else {
                result = this.page(result, new LambdaQueryWrapper<Class>());
            }
        }
        return result;
    }

    @Override
    public List<Class> listWithProfessor() {
        List<Class> list = this.list();
        if (!CollectionUtils.isEmpty(list)) {
            final List<Integer> collect = list.stream().map(x -> x.getProfessorId()).collect(Collectors.toList());
            final List<Professor> professors = professorService.listByIds(collect);
            final Map<Integer, String> map = professors.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getName()));
            for (Class aClass : list) {
                aClass.setProfessorName(map.get(aClass.getProfessorId()));
            }
        }
        return list;
    }
}
